﻿using Generic.App.Domain.Entity;
using Generic.Helper.Configure;
using Generic.Helper.Cryptography;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Diagnostics;
using Volo.Abp.EntityFrameworkCore;

namespace Generic.App.EntityFrameworkCore
{
    [Volo.Abp.Data.ConnectionStringName("GenericAppConnectionStr")]
    public partial class GenericAppContext : AbpDbContext<GenericAppContext>
    {
        public readonly IOptions<AesOptions> _aesOptions;
        public GenericAppContext(DbContextOptions<GenericAppContext> options, IOptions<AesOptions> aesOptions) : base(options)
        {
            _aesOptions = aesOptions;
        }

        public virtual DbSet<App_Account> App_Account { get; set; }
        public virtual DbSet<App_TokenGrey> App_TokenGrey { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.LogTo(message => Debug.WriteLine(message), LogLevel.Debug); //出现在“输出 - 调试”窗口
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //Seed Data
            string appSecretSeed = "123456".AesEncypt(_aesOptions.Value.Key, _aesOptions.Value.IV);
            modelBuilder.Entity<App_Account>().HasData(new App_Account
            {
                Id = "6a904a8b-e847-459c-86fc-94ecdbe174e3",
                AppId = "Test",
                AppSecret = appSecretSeed,
                IsActive = true
            });

            base.OnModelCreating(modelBuilder);
        }

    }
}
